<template>
  <div class="agent-reconciliation">
    <el-card>
      <template #header>
        <div class="card-header">
          <span class="title">🤝 代理对账</span>
          <div class="header-actions">
            <el-button type="primary" @click="handleGenerate">
              <el-icon><DocumentAdd /></el-icon>
              生成对账单
            </el-button>
            <el-button type="success" @click="handleBatchSettle">
              <el-icon><Money /></el-icon>
              批量结算
            </el-button>
          </div>
        </div>
      </template>

      <!-- 搜索区域 -->
      <el-form :inline="true" class="search-form">
        <el-form-item label="代理商">
          <el-input v-model="searchForm.agentName" placeholder="请输入代理商名称" clearable />
        </el-form-item>
        <el-form-item label="代理级别">
          <el-select v-model="searchForm.level" placeholder="请选择级别" clearable>
            <el-option label="一级代理" value="1" />
            <el-option label="二级代理" value="2" />
            <el-option label="三级代理" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="对账周期">
          <el-date-picker
            v-model="searchForm.period"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 统计概览 -->
      <el-row :gutter="20" class="stat-overview">
        <el-col :span="6">
          <el-statistic title="应收总额" :value="3856234" prefix="¥" />
        </el-col>
        <el-col :span="6">
          <el-statistic title="已收金额" :value="3456789" prefix="¥" value-style="color: #67c23a" />
        </el-col>
        <el-col :span="6">
          <el-statistic title="未收金额" :value="399445" prefix="¥" value-style="color: #f56c6c" />
        </el-col>
        <el-col :span="6">
          <el-statistic title="代理商数" :value="156" />
        </el-col>
      </el-row>

      <!-- 对账列表 -->
      <el-table :data="tableData" border stripe>
        <el-table-column type="selection" width="55" />
        <el-table-column prop="id" label="对账单号" width="150" />
        <el-table-column prop="agentName" label="代理商名称" width="200" />
        <el-table-column prop="level" label="级别" width="100">
          <template #default="{ row }">
            <el-tag>{{ row.level }}级代理</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="region" label="代理区域" width="150" />
        <el-table-column prop="period" label="对账周期" width="180">
          <template #default="{ row }">
            {{ row.startDate }} 至 {{ row.endDate }}
          </template>
        </el-table-column>
        <el-table-column prop="salesAmount" label="销售额" width="130">
          <template #default="{ row }">
            ¥{{ row.salesAmount.toLocaleString() }}
          </template>
        </el-table-column>
        <el-table-column prop="commission" label="佣金" width="120">
          <template #default="{ row }">
            <span style="color: #e6a23c; font-weight: bold;">¥{{ row.commission.toLocaleString() }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="receivable" label="应收款" width="130">
          <template #default="{ row }">
            <span style="color: #409eff; font-weight: bold;">¥{{ row.receivable.toLocaleString() }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusTag(row.status)">
              {{ getStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="250" fixed="right">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleViewDetail(row)">查看明细</el-button>
            <el-button link type="success" @click="handleConfirm(row)">确认</el-button>
            <el-button link type="info" @click="handleExport(row)">导出</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        v-model:current-page="pagination.page"
        v-model:page-size="pagination.size"
        :total="pagination.total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSearch"
        @current-change="handleSearch"
      />
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { DocumentAdd, Money } from '@element-plus/icons-vue'

const searchForm = reactive({
  agentName: '',
  level: '',
  period: null
})

const tableData = ref([
  {
    id: 'DZ-AGT-202401001',
    agentName: '华南总代理',
    level: 1,
    region: '广东省',
    startDate: '2024-01-01',
    endDate: '2024-01-31',
    salesAmount: 2456789,
    commission: 245679,
    receivable: 2211110,
    status: 'confirmed'
  },
  {
    id: 'DZ-AGT-202401002',
    agentName: '深圳一级代理',
    level: 2,
    region: '深圳市',
    startDate: '2024-01-01',
    endDate: '2024-01-31',
    salesAmount: 1234567,
    commission: 123457,
    receivable: 1111110,
    status: 'pending'
  }
])

const pagination = reactive({
  page: 1,
  size: 10,
  total: 2
})

const getStatusTag = (status) => {
  return status === 'confirmed' ? 'success' : 'warning'
}

const getStatusText = (status) => {
  return status === 'confirmed' ? '已确认' : '待确认'
}

const handleSearch = () => {
  ElMessage.success('查询成功')
}

const handleReset = () => {
  Object.assign(searchForm, { agentName: '', level: '', period: null })
}

const handleGenerate = () => {
  ElMessage.info('生成对账单')
}

const handleViewDetail = () => {
  ElMessage.info('查看明细')
}

const handleConfirm = () => {
  ElMessage.success('确认成功')
}

const handleExport = (row) => {
  ElMessage.success(`正在导出对账单 ${row.id}`)
}

const handleBatchSettle = () => {
  ElMessage.info('批量结算')
}
</script>

<style scoped>
.agent-reconciliation {
  padding: 20px;
  background: #f5f5f5;
  min-height: 100vh;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.title {
  font-size: 18px;
  font-weight: bold;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.search-form {
  margin-bottom: 20px;
}

.stat-overview {
  margin-bottom: 20px;
  padding: 20px;
  background: white;
  border-radius: 8px;
}

.el-pagination {
  margin-top: 20px;
  justify-content: flex-end;
}
</style>

